<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
 
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CateController extends Controller
{

	//显示
    public function getIndex(Request $request){
        if($request['name']){
            //搜索分页
            $res = DB::table('group')
            ->join('cate','cate.ClassId','=','group.ClassId')
            ->where('CateName','like','%'.$request['name'].'%')
            ->paginate(10);
        }else{
            //所有分页

            $res = DB::table('group')
            ->join('cate','cate.ClassId','=','group.ClassId')
            ->orderBy('cate.CateId','asc')
            ->paginate(10);

        }
        // dd($info);
        $info = $request -> all();
        
        return view('/cate/cate',['res'=>$res,'info'=>$info]);
    }


    

    //添加
    public function getAdd(){
    	$res = DB::table('group')->get();
    	// dd($res);
        return view('/cate/add',['res'=>$res]);

    }
    //执行添加
    public function postInsert(Request $request){
         $this->validate($request, [
        'CateName' => 'required',
        'Remark' => 'required',
        ],[
        'CateName.required'=>'菜单名称不能为空',
        'Remark.required'=>'备注不能为空',
        ]);
    	$res = $request->only(['ClassId','CateName','Remark']);
    	// dd($res);
    	DB::table('cate')->insert($res);
    	return redirect('/cate');
    }

    //删除
    public function getDel($id){
    	// dd($id);
    	DB::table('cate')->where('CateId',$id)->delete();
    	return redirect('/cate');
    	
    }

    //编辑
    public function getEdit($id){
    	$res = DB::table('group')
    	->join('cate','cate.ClassId','=','group.ClassId')
    	->where('cate.CateId',$id)
    	->get();
    	// dd($res);
    	$ClassName = DB::table('group')->get();
    	// dd($ClassName);
        return view('/cate/edit',['res'=>$res,'ClassName'=>$ClassName]);

    }

    //执行修改
    public function postUpdate(Request $request){
    	$res = $request->only(['CateId','CateName','ClassId','Remark']);
    	// dd($res);
    	DB::table('cate')->where('CateId',$res['CateId'])->update($res);
    	return redirect('/cate');

    }
}
